package com.lilong.jwt.common.base;

import com.lilong.jwt.common.util.ResultBean;
import com.lilong.jwt.common.util.ResultUtil;
import com.lilong.jwt.pojo.query.ListBean;
import com.lilong.jwt.pojo.query.RequestBean;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;

/**
 * 实现基本Controller
 * @param <T>
 */
public abstract class CommonController<T extends BaseEntity> extends BaseController {
    protected abstract BaseService<T> getService();

    /**
     * 新增数据
     */
    @PostMapping("add")
    public ResultBean add(@RequestBody T t) {
        return ResultUtil.SUCCESS(getService().saveEntity(t, getOperator()).getId());
    }

    /**
     *修改数据
     */
    @PutMapping("edit")
    public ResultBean edit(@RequestBody T t) {
        return ResultUtil.SUCCESS(getService().updateEntity(t, getOperator()));
    }

    /**
     * 删除数据
     */
    @DeleteMapping("delete")
    public ResultBean delete(@RequestBody ListBean<Long> listBean) {
        return ResultUtil.SUCCESS(getService().batchRemoveEntity(listBean.getData()));
    }

    /**
     * 分页查询数据
     */
    @PostMapping("page")
    public ResultBean page(@RequestBody RequestBean<T> requestBean) {
        return ResultUtil.SUCCESS(getService().queryByPage(requestBean));
    }

    /**
     * 普通查询
     */
    @PostMapping("list")
    public ResultBean list(@RequestBody T t) {
        return ResultUtil.SUCCESS(getService().query(t));
    }

    /**
     * 查看数据详情
     */
    @PostMapping("view")
    public ResultBean view(Long id) {
        return ResultUtil.SUCCESS(getService().findById(id));
    }
}
